package com.ruoyi.sms.common;

import com.itextpdf.text.BadElementException;
import com.ruoyi.bizsys.domain.SmsAreaNumberConfig;
import com.ruoyi.bizsys.domain.SmsTemplate;
import com.ruoyi.bizsys.service.ISmsAreaNumberConfigService;
import com.ruoyi.bizsys.service.ISmsTemplateService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @desc 短信字典配置项
 * @author pgc
 * @date 2021-10-27
 */
@Slf4j
@Component
public class SmsTool {

    @Autowired
    public ISysDictDataService iSysDictDataService;

    @Autowired
    public ISysDictTypeService iSysDictTypeService;

    @Autowired
    RedisCache redisCache;

    @Autowired
    ISmsAreaNumberConfigService iSmsAreaNumberConfigService;

    @Autowired
    ISmsTemplateService iSmsTemplateService;

    //短信运营商优先级
    public static final String SMS_OPEROTAR_PRIORITY = "sys_dict:sms_operator_priority";

    //短信类型
    public static final String SMS_TYPE_CONFIG = "sys_dict:sms_type_config";

    //运营商号段
    public static final String SMS_AREA_NUMBER = "sms_area:smsAreaNumber";
    //各号段运营商优先级
    public static final String SMS_AREA_NUMBER_PRIORITY = "sms_area:smsAreaNumber_priority";

    //定时任务短信模板执行时间
    public static final String SMS_TEMPLATE_TIME= "smsTemplateTime";

    //字典查询状态
    public static final String SYS_DICT_DATA_STATUS= "0";

    //短信号段查询状态
    public static final int SMS_AREA_NUMBER_STATUS= 1;

    //AT服务商状态码
    public static final String AT_SMS_CODE = "sys_dict:sms_africastalking_resp_code";

    //webeye服务商状态码
//    public static final String WEBEYE_SMS_CODE = "";

    //bateSMS服务商状态码
    public static final String BETASMS_SMS_CODE = "sys_dict:sms_betasms_resp_code";

    //infobip 服务商状态码
    public static final String INFPBIP_SMS_CODE = "sys_dict:sms_infobip_resp_code";

    //Multitexter 服务商状态码
    public static final String MULTITEXTER_SMS_CODE = "sys_dict:sms_multitexter_resp_code";

    //buka 服务商状态码
    public static final String BUKA_SMS_CODE = "sys_dict:sms_buka_resp_code";

    //短信运营商挡板配置
    public static final String SMS_BAFFLE_CONFIG = "sys_dict:sms_baffle_config";

    //短信定时任务流转间隔时间
    public static final String SMS_INTERVAL_TIME = "sys_dict:sms_interval_time";

    //将配置项放入缓存
    // @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("--------- >>> init sms config start : " + System.currentTimeMillis());
        //运营商优先级规则缓存
        SysDictData sysDictData = new SysDictData();
//        sysDictData.setDictType(SMS_OPEROTAR_PRIORITY);
        sysDictData.setStatus(SYS_DICT_DATA_STATUS);
        List<SysDictData> sysDictDataList = iSysDictDataService.selectDictDataList(sysDictData);
        //运营商优先级
        Map<Integer,String> map = new TreeMap<Integer,String>();
        //短信类型添加缓存
        List<String> typeList = new ArrayList<String>();
        //AT 响应状态码
        Map<Integer,String> atMap = new HashMap<>();
        //infobip 响应状态码
        Map<Integer,String> infobipMap = new HashMap<>();
        //betaSMS响应状态码
        Map<Integer,String> betaSMSMap = new HashMap<>();
        //multitexter 响应状态码
        Map<Integer,String> multitexterMap = new HashMap<>();
        //buka 响应状态码
        Map<Integer,String> bukaMap = new HashMap<>();
        //各号段运营商优先级map
        Map<String,String> priorityMap = new HashMap<>();

        //短信定时任务间隔时间
        int intervalTime = 0;
        for(int i=0; i<sysDictDataList.size(); i++) {
            switch (sysDictDataList.get(i).getDictType()){
                case SMS_OPEROTAR_PRIORITY://运营商优先级
                    map.put(Integer.parseInt(sysDictDataList.get(i).getDictValue()),
                            sysDictDataList.get(i).getDictLabel()+","+sysDictDataList.get(i).getRemark());
                    break;
                case SMS_TYPE_CONFIG://短信类型添加缓存
                    typeList.add(sysDictDataList.get(i).getDictLabel());
                    break;
                case AT_SMS_CODE://AT 运营商需要流转的状态码
                    atMap.put(Integer.parseInt(sysDictDataList.get(i).getDictLabel()),sysDictDataList.get(i).getDictValue());
                    break;
                case INFPBIP_SMS_CODE://infobip 运营商需要流转的状态码
                    infobipMap.put(Integer.parseInt(sysDictDataList.get(i).getDictLabel()),sysDictDataList.get(i).getDictValue());
                    break;
                case BETASMS_SMS_CODE://betasms 运营商需要流转的状态码
                    betaSMSMap.put(Integer.parseInt(sysDictDataList.get(i).getDictLabel()),sysDictDataList.get(i).getDictValue());
                    break;
                case MULTITEXTER_SMS_CODE://multitexter 运营商需要流转的状态码
                    multitexterMap.put(Integer.parseInt(sysDictDataList.get(i).getDictLabel()),sysDictDataList.get(i).getDictValue());
                    break;
                case BUKA_SMS_CODE://buka 运营商
                    bukaMap.put(Integer.parseInt(sysDictDataList.get(i).getDictLabel()),sysDictDataList.get(i).getDictValue());
                    break;
                case SMS_INTERVAL_TIME://短信定时任务流转间隔时间
                    intervalTime = Integer.parseInt(sysDictDataList.get(i).getDictValue()!=null
                            ? sysDictDataList.get(i).getDictValue() : "0");
                    break;
            }
        }
        redisCache.setCacheObject(SMS_OPEROTAR_PRIORITY,map);
        redisCache.setCacheObject(SMS_TYPE_CONFIG,typeList);
        redisCache.setCacheObject(AT_SMS_CODE,atMap);
        redisCache.setCacheObject(INFPBIP_SMS_CODE,infobipMap);
        redisCache.setCacheObject(BETASMS_SMS_CODE,betaSMSMap);
        redisCache.setCacheObject(MULTITEXTER_SMS_CODE,multitexterMap);
        redisCache.setCacheObject(SMS_INTERVAL_TIME,intervalTime);
        redisCache.setCacheObject(BUKA_SMS_CODE,bukaMap);
        //运营商号段
        SmsAreaNumberConfig smsAreaNumberConfig = new SmsAreaNumberConfig();
        smsAreaNumberConfig.setStatus(SMS_AREA_NUMBER_STATUS);
        List<SmsAreaNumberConfig> smsAreaNumberConfigList =
                iSmsAreaNumberConfigService.selectSmsAreaNumberConfigList(smsAreaNumberConfig);
        List<String> smsNumberList = new ArrayList<String>();
        for(int i=0; i<smsAreaNumberConfigList.size(); i++){
            smsNumberList.add(smsAreaNumberConfigList.get(i).getAreaNumber());
            priorityMap.put(smsAreaNumberConfigList.get(i).getAreaNumber(),
                    smsAreaNumberConfigList.get(i).getSmsMarketingMode());
        }
        redisCache.setCacheObject(SMS_AREA_NUMBER,smsNumberList);
        redisCache.setCacheObject(SMS_AREA_NUMBER_PRIORITY,priorityMap);

        //批量短信定时任务时间添加至redis缓存, 业务设计定时任务执行时间为整数
        SmsTemplate smsTemplate = new SmsTemplate();
        smsTemplate.setState("1");
        List<SmsTemplate> smsTemplateList = iSmsTemplateService.selectSmsTemplateList(smsTemplate);
        Set<Integer> set = new HashSet<Integer>();
        for (SmsTemplate smsTem : smsTemplateList){
            if(null !=smsTem.getSendClock() && !"".equals(smsTem.getSendClock())){
                String[] smsTemplateArray = smsTem.getSendClock().split(",");
                for(int i=0; i<smsTemplateArray.length; i++){
                    set.add(Integer.valueOf(smsTemplateArray[i]));
                }
            }
        }
        redisCache.setCacheObject(SMS_TEMPLATE_TIME,set);
        //短信测试挡板
        SysDictType sysDictType = iSysDictTypeService.selectDictTypeByType(SMS_BAFFLE_CONFIG);
        if(null !=sysDictType){
            redisCache.setCacheObject(SMS_BAFFLE_CONFIG,sysDictType.getStatus());
        }

        log.info("--------- >>> init sms config end : " + System.currentTimeMillis());
    }
}
